{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "edb6ed95",
   "metadata": {},
   "source": [
    "# Visualization of TreeValue"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fb09e8c",
   "metadata": {},
   "source": [
    "Create a simple `FastTreeValue` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b982cde7",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install treevalue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc7e3dd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from treevalue import FastTreeValue\n",
    "\n",
    "t = FastTreeValue({\n",
    "    'a': 1,\n",
    "    'b': np.random.randint(0, 20, (3, 4)),\n",
    "    'x': {\n",
    "        'c': 3,\n",
    "        'd': torch.randn(2, 3),\n",
    "    },\n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b654200",
   "metadata": {},
   "source": [
    "See it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30771752",
   "metadata": {},
   "outputs": [],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2c1bb61",
   "metadata": {},
   "source": [
    "Do some operations on `t`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc97351a",
   "metadata": {},
   "outputs": [],
   "source": [
    "1 / (t + 0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b25f3db4",
   "metadata": {},
   "source": [
    "Create another `FastTreeValue` which is sharing storage with `t`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af6345dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "t2 = FastTreeValue({'ppp': t.x, 'x': {'t': t, 'y': t.x}})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e039d8d9",
   "metadata": {},
   "source": [
    "Just put `t` and `t2` into one diagram."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57446c40",
   "metadata": {},
   "outputs": [],
   "source": [
    "from treevalue import graphics\n",
    "\n",
    "graphics((t, 't'), (t2, 't2'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
